package Database;

import java.sql.*;
import java.util.Vector;
import javax.swing.text.StyledEditorKit.BoldAction;

public class DanhMuc 
{
    private String madm;
    private String tendm;

    /**
     * @return the madm
     */
    public String getMadm() {
        return madm;
    }

    /**
     * @param madm the madm to set
     */
    public void setMadm(String madm) {
        this.madm = madm;
    }

    /**
     * @return the tendm
     */
    public String getTendm() {
        return tendm;
    }

    /**
     * @param tendm the tendm to set
     */
    public void setTendm(String tendm) {
        this.tendm = tendm;
    }
    
    public boolean ThemDanhMuc(String madm, String tendm)
    {
        boolean kq = true;
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "insert into danhmuc values('"+madm+"', '"+tendm+"')";
            Statement st = con.createStatement();
            kq = st.executeUpdate(query) > 0;
        }
        catch(Exception ex)
        {
            kq = false;
        }
        
        return kq;
    }
    
    public boolean ThemDanhMuc()
    {
        boolean kq = true;
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "insert into danhmuc values('"+this.madm+"', '"+this.tendm+"')";
            Statement st = con.createStatement();
            kq = st.executeUpdate(query) > 0;
        }
        catch(Exception ex)
        {
            kq = false;
        }
        
        return kq;
    }
    
    public boolean CapNhatDanhMuc()
    {
        boolean kq = true;
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "update danhmuc set tendm='"+this.tendm+"' where madm='"+this.madm+"'";
            Statement st = con.createStatement();
            kq = st.executeUpdate(query) > 0;
        }
        catch(Exception ex)
        {
            kq = false;
        }
        
        return kq;
    }
    
    /** Dung de xoa danh muc
     *  Tham so: Truyen ma danh muc kieu chuoi cho ham thong qua
     *  thuoc tinh
     *  @return : kieu boolean
     */
    public boolean XoaDanhMuc()
    {
        boolean kq = true;
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "delete from danhmuc where madm='"+this.madm+"'";
            Statement st = con.createStatement();
            kq = st.executeUpdate(query) > 0;
        }
        catch(Exception ex)
        {
            kq = false;
        }
        
        return kq;
    }
    
    /**
     * Lay danh sach danh muc  
     */
    public Vector<Vector> DanhSachDanhMuc()
    {
        Vector<Vector> dsdm = new Vector<Vector>();
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from danhmuc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while(rs.next())
            {
                Vector<Object> dm = new Vector<Object>();
                dm.add(rs.getString("madm"));
                dm.add(rs.getString("tendm"));
                dsdm.add(dm);
            }
        }
        catch(Exception ex)
        {
            dsdm = null;
        }
        
        return dsdm;
    }
    
    /**
     * Lay thong tin cua 1 danh muc
     */
    
    public DanhMuc ThongTinDanhMuc()
    {
        DanhMuc dm = new DanhMuc();
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from danhmuc where madm = '"+this.madm+"'";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while(rs.next())
            {
                dm.setMadm(rs.getString("madm"));
                dm.setTendm(rs.getString("tendm"));
            }
        }
        catch(Exception ex)
        {
            dm = null;
        }
        
        return dm;
    }
 
    public Vector<DanhMuc> DanhSachDM()
    {
        Vector<DanhMuc> dsdm = new Vector<DanhMuc>();        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "select * from danhmuc";
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery(query);
            while(rs.next())
            {
                DanhMuc dm = new DanhMuc();
                dm.setMadm(rs.getString("madm"));
                dm.setTendm(rs.getString("tendm"));
                dsdm.add(dm);
            }
        }
        catch(Exception ex)
        {
            dsdm = null;
        }
        
        return dsdm;
    }
    
    public boolean ThemDM()
    {
        boolean kq = true;
        
        try
        {
            Connection con = ConnectToDatabase.getConnection();
            String query = "insert into danhmuc values(?, ?)";
            PreparedStatement ps = con.prepareStatement(query);
            ps.setString(1, this.madm);
            ps.setString(2, this.tendm);
            kq = ps.executeUpdate() > 0;
        }
        catch(Exception ex)
        {
            kq = false;
        }
        
        return kq;
    }
    
}
